//
//  CZNetworkTools.swift
//  06-Swift之AFN网络封装
//
//  Created by smile on 2017/7/31.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import AFNetworking
// 请求方式
enum CZNetworkToolsType:String {
    case get = "get"
    case post = "post"
}

class CZNetworkTools: AFHTTPSessionManager {

    // 全局访问点
    static let share: CZNetworkTools = {
        let tools = CZNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
//        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools
    }()
    
    
    /// 公共请求方法
    ///
    /// - Parameters:
    ///   - type: get-post
    ///   - urlString: 请求地址
    ///   - parameters: 请求参数
    ///   - success: 成功
    ///   - failure: 失败
    func requst(type: CZNetworkToolsType, urlString: String, parameters: Any?, success:@escaping (Any?)->(), failure:@escaping(Error)->()){
        // 判断请求方式
        // get
        if type == .get {
            get(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response);
            }, failure: { (_, error) in
                failure(error)
            })
        }
        // post
        if type == .post{
            post(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response);
            }, failure: { (_, error) in
                failure(error)
            })
        }

    }
    
}
// MARK: - Home模块的网络请求
extension CZNetworkTools{
    func getHome(since_id:Int64, max_id: Int64, success:@escaping (Any?)->(), failure:@escaping(Error)->()){
        // 请求的地址
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求的参数
        let params = [
            "access_token": CZOAuthViewModel.share.accessToken!,
            "since_id": "\(since_id)",
            "max_id": "\(max_id)"
        ]
//        
//        	false	int64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
//        	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0。
        // 发送请求
        requst(type: CZNetworkToolsType.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}


// MARK: - OAuth模块的网络请求
extension CZNetworkTools{
    func getOAuthUserAccount(code: String, success:@escaping (Any?)->(), failure:@escaping(Error)->()){
        // 请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": CZAppKey,
            "client_secret": CZAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": CZRedirect_uri
        ]
        // 发送网络请求
        requst(type: .post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    func getOAuthUserInfo(userAccountModel: CZUserAccountModel, success:@escaping (Any?)->(), failure:@escaping(Error)->()){
        // 请求地址
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token": userAccountModel.access_token!,
            "uid": userAccountModel.uid!
        ]
        // 发送网络请求
        requst(type: CZNetworkToolsType.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}
